<?php

namespace App\Http\Controllers\Home;

use App\Model\HomeUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller
{
    //
    public function login()
    {
    	return view('home.login');
    }

    public function doLogin(Request $request)
	{
		// 接收表单数据
		$data = $request->except('_token');
		// dd($data);

		// 验证规则
		// $rule = [
		// 	'username' => 'required|between:4,20',
		// 	'password' => 'required|between:6,12|alpha_dash',
		// ];

		// 进行表单验证
		// $validator = Validator::make($data, $rule);

        // if ($validator->fails()) {
            // return redirect('admin/login')
                        // ->withErrors($validator)
                        // ->withInput();
        // }

		// 验证管理员信息是否存在
		$user = HomeUser::where('user_name',$data['username'])->first();
		if (!$user) {
			# code...
			return redirect('/login');//->with('errors','用户名错误!!!');
		}
		// 验证密码是否一致
		if (!Hash::check($data['password'],$user->password)) {
			# code...
			return redirect('/login');//->with('errors','密码错误!!!');			
		}

		// 将登录信息存入session
		session()->push('homeuser', $user);

		// 跳转至后台首页
		return redirect('/');
	}
}
